package com.pipe.protocol.events.common;

import io.netty.buffer.ByteBuf;

/**
 * @author Kiven
 */
public class QueryEventPayLoad {

    private String statusVars;
    private String schema;
    private String query;

    public QueryEventPayLoad(String statusVars, String schema, String query) {
        this.statusVars = statusVars;
        this.schema = schema;
        this.query = query;
    }

    public static QueryEventPayLoad build(int statusVarsLength, int schemaLen, ByteBuf bytebuf){

        byte[] statusVars = new byte[statusVarsLength];
        bytebuf.readBytes(statusVars);

        byte[] schema = new byte[schemaLen];
        bytebuf.readBytes(schema);

        bytebuf.readByte();//skip 00

        byte[] query = new byte[bytebuf.readableBytes()];
        bytebuf.readBytes(query);

        return new QueryEventPayLoad(new String(statusVars), new String(schema), new String(query));
    }

    @Override
    public String toString() {
        return "QueryEventPayLoad{" +
                "statusVars='" + statusVars + '\'' +
                ", schema='" + schema + '\'' +
                ", query='" + query + '\'' +
                '}';
    }
}